import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import api from '../api/axiosConfig';
import axios from 'axios';

const CreateTeamPage = () => {
    const [name, setName] = useState('');
    const [description, setDescription] = useState('');
    const [error, setError] = useState('');
    const navigate = useNavigate();

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        try {
            await api.post('/teams', { name, description });
            navigate('/my-teams'); // 成功后跳转到小队管理页
        } catch (err) {
            if (axios.isAxiosError(err)) {
                setError(err.response?.data?.message || '创建失败');
            } else {
                setError('发生未知错误');
            }
        }
    };

    return (
        <div style={{ maxWidth: '600px', margin: '0 auto' }}>
            <h2>创建新小队</h2>
            <form onSubmit={handleSubmit}>
                <div style={{ marginBottom: '1rem' }}>
                    <label>小队名称:</label>
                    <input type="text" value={name} onChange={(e) => setName(e.target.value)} required style={{ width: '100%', padding: '8px' }}/>
                </div>
                <div style={{ marginBottom: '1rem' }}>
                    <label>小队简介:</label>
                    <textarea value={description} onChange={(e) => setDescription(e.target.value)} style={{ width: '100%', padding: '8px', minHeight: '100px' }}/>
                </div>
                {error && <p style={{ color: 'red' }}>{error}</p>}
                <button type="submit">创建</button>
            </form>
        </div>
    );
};

export default CreateTeamPage;